package com.jumi.microservice.common.core.aspect.handler;

import cn.hutool.core.util.StrUtil;
import com.alibaba.druid.pool.DruidDataSource;
import com.google.common.base.CaseFormat;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * - - - - - - - - - - - - - - -
 * jdbc获取数据工具类
 * @author liuz
 * @date 2020/12/4 14:29
 * ToDoSomeThing
 * - - - - - - - - - - - - - - -
 */
@Component
public class DataObjectUtil {

    /**
     * 获取数据
     * @param sql 执行sql
     * @param entityType 返回实体类类型
     * @param druidDataSource 数据库类型
     * @return
     */
    public List<?> getOldData(String sql, Class<?> entityType, DruidDataSource druidDataSource){
        JdbcTemplate jdbcTemplate=new JdbcTemplate(druidDataSource);
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        return setList(list, entityType);
    }

    /**
     * 数据转化
     * @param srcList
     * @param clazz
     * @param <T>
     * @return
     */
    private static <T> List<T> setList(List<Map<String, Object>> srcList, Class<T> clazz) {
        List<T> list = new ArrayList<>();
        srcList.forEach(x -> {
            try {
                T t = clazz.newInstance();
                Field[] fields = t.getClass().getDeclaredFields();
                for (Field field : fields) {
                    if (!"serialVersionUID".equals(field.getName())) {
                        //设置对象的访问权限，保证对private的属性的访问
                        field.setAccessible(true);
                        //读取配置转换字段名，并从map中取出数据
                        Object v = x.get(getUnder(field.getName()));
                        field.set(t, convert(v, field.getType()));
                    }
                }
                list.add(t);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        });
        return list;
    }

    /**
     * Field类型转换
     * @param obj
     * @param type
     * @param <T>
     * @return
     */
    private static <T> T convert(Object obj, Class<T> type) {
        if (obj != null && StrUtil.isNotBlank(obj.toString())) {
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            if (type.equals(String.class)) {
                return (T) obj.toString();
            } else if (type.equals(BigDecimal.class)) {
                return (T) new BigDecimal(obj.toString());
            }else if(type.equals(LocalDateTime.class)){
                return (T) LocalDateTime.parse(obj.toString().substring(0,obj.toString().indexOf(".")), dtf);
            }else if(type.equals(Long.class)){
                return (T) new Long(obj.toString());
            } else if (type.equals(Integer.class)) {
                if("true".equals(obj.toString())){
                    return (T) new Integer(1);
                }else if("false".equals(obj.toString())){
                    return (T) new Integer(0);
                }
                return (T) new Integer(obj.toString());
            }else if (type.equals(Date.class)){
                return (T) new Date(obj.toString());
            }else if (type.equals(Boolean.class)){
                if("true".equals(obj.toString())){
                    return (T) new Integer(1);
                }else if("false".equals(obj.toString())){
                    return (T) new Integer(0);
                }
            }
        }
        return null;
    }

    private static String getUnder(String fild){
        return CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, fild);
    }
}
